export default function (rawData, interval = 4, step = 4, method = 0) {
  let output = new Array();

  // 判断cut方式
  let targetIndex = 0;
  let targetMehod = "index";
  if (Number.isInteger(method) && method < step) {
    targetIndex = method;
  } else if (
    typeof method == "string" &&
    ((method == "more" &&
      (typeof rawData[0] == "number" || typeof rawData[0] == "string") &&
      !isNaN(rawData[0])) ||
      ([
        "max",
        "min",
        "avg",
        "avgCeil",
        "avgFloor",
        "avgCeil",
        "avgRaw",
      ].includes(method) &&
        typeof rawData[0] == "number" &&
        !isNaN(rawData[0])))
  ) {
    targetMehod = method;
  }

  for (let i = 0; i < rawData.length; i += interval) {
    let cell = JSON.parse(JSON.stringify(rawData[i]));
    if (
      targetMehod == "index" &&
      targetIndex != 0 &&
      i + targetIndex < rawData.length
    ) {
      cell = JSON.parse(JSON.stringify(rawData[i + targetIndex]));
      // console.log("stepCut", cell);
    } else if (targetMehod.includes("avg")) {
      let avgCount = 1;
      for (let j = 1; j < step; j++) {
        if (i + j < rawData.length) {
          avgCount++;
          cell += rawData[i + j];
        }
      }
      cell = cell / avgCount;
      if (targetMehod == "avgFloor") {
        cell.Math.floor(cell);
      } else if (targetMehod == "avgCeil") {
        cell = Math.ceil(cell);
      } else if (targetMehod == "avgRound") {
        cell = Math.round(cell);
      }
    } else {
      let cellList = [cell];
      for (let j = 1; j < step; j++) {
        if (i + j < rawData.length) {
          cellList.push(rawData[i + j]);
        }
      }
      // console.log("stepCut", "cellList", cellList);
      if (targetMehod == "max") {
        cell = Math.max(...cellList);
      } else if (targetMehod == "min") {
        cell = Math.min(...cellList);
      } else if (targetMehod == "more") {
        let moreList = {};
        cellList.forEach((moreValue) => {
          let moreKey = "val_" + moreValue;
          if (!moreList[moreKey]) {
            moreList[moreKey] = { value: moreValue, count: 1 };
          } else {
            moreList[moreKey].count++;
          }
        });
        let moreValue = null;
        let moreCount = 0;
        Object.keys(moreList).forEach((moreKey) => {
          if (moreValue === null || moreList[moreKey].count >= moreCount) {
            moreCount = moreList[moreKey].count;
            moreValue = moreList[moreKey].value;
          }
        });
        cell = moreValue;
      }
    }
    // console.log("stepCut", "targetMehod:" + targetMehod, cell);
    output.push(cell);
  }
  return output;
}
